package practise;

import java.util.Stack;

public class InvertTree {
    public TreeNode invertTree(TreeNode root) {
        if(root == null) {
            return null;
        }

        Stack<TreeNode> stack = new Stack<>();
        stack.push(root);
        while(!stack.isEmpty()) {
            TreeNode root1 = stack.pop();
            if(root1 != null) {
                TreeNode tmp = root1.left;
                root1.left = root1.right;
                root1.right = tmp;
                stack.push(root1.left);
                stack.push(root1.right);
            }
        }
        return root;
    }
}
